﻿<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title>角色权限管理</title>
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <link href="../lib/layui/lay/plugins/eleTree/font/eleTree.css" rel="stylesheet" />
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script type="text/javascript" src="../linq.js/linq.js"></script>
    <script src="../lib/layui/layui.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <form class="layui-form" lay-filter="authorityForm">
            <input type="hidden" id="rid" name="rid"/>
            <div class="layui-form-item layui-form-text" style="min-height:450px;">
                <div class="layui-input-inline">
                    <div class="eleTree" id="authorityTree" lay-filter="authorityTree"></div>
                </div>
            </div>
            <div class="layui-form-item" style="text-align:center;" >
                <a class="layui-btn" href="javascript:;" lay-submit lay-filter="saveauthority">保存</a>
            </div>
        </form>
        
    </div>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        };
        layui.$(function () {
            layui.$("#rid").val(getrequest().rid);
        })

        layui.config({
            base: '/lib/layui/lay/plugins/'
        }).extend({
            eleTree: 'eleTree/eleTree'
        }).use(['eleTree','form'], function () {
            var eleTree = layui.eleTree;
            var form = layui.form;
            var el = eleTree({
                el: '#authorityTree',
                method: 'get',
                url: localStorage.getItem('hosttingaddr') + '/api/role/GetAuthorityTree?rid=' + getrequest().rid,
                emptText:"暂无数据",
                showCheckbox: true,
                highlightCurrent: true,
                showLine: true,
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    dataName: 'data'
                },
                request: {
                    name: 'name',
                    key: 'id',
                    children: 'childrens',
                    checked: 'checked',
                    disabled: 'disabled',
                    isLeaf: 'isLeaf'
                },
                done: function (res) {
                    el.setChecked(res.checkedIds);
                }
            });

            //监听提交 
            form.on('submit(saveauthority)', function (data) {
                data.elem.removeAttribute("lay-submit");
                var data = data;
                var authoritys = el.getChecked(false, true);
                var aidArry = new Array();
                if (authoritys != null && authoritys.length>0) {
                    for (var i = 0; i < authoritys.length; i++) {
                        aidArry.push(authoritys[i].id)
                    }
                }
                data.field.Aids = aidArry;
                data.field.IsClearAll = 0;
                var i = layui.layer.load();
                
                layui.$.ajax({
                    type: 'post',
                    url: localStorage.getItem('hosttingaddr') + '/api/role/saveroleauthority',
                    data: data.field,
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(data.field),
                    success: function (re) {
                        layui.layer.close(i);
                        if (re.code == 0) {
                            layer.msg("保存成功!", { icon: 1, time: 1000 }, function () {
                                data.elem.setAttribute("lay-submit", true);
                                var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                parent.layer.close(index);
                            });
                        }
                    },
                    error: function (re) {
                        layui.layer.close(i);
                        layer.msg("保存失败!", { icon: 1, time: 1000 }, function () {
                            data.elem.setAttribute("lay-submit", true);
                            var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                            parent.layer.close(index);
                        });
                    }
                });
                return false;
            });

        })
    </script>
</body>
</html>